package com.itmate.chenshiguang.contreolle;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.itmate.chenshiguang.entity.member;	
import com.itmate.chenshiguang.service.MenberUserService;



/**
 * Servlet implementation class DoRegister
 */
@WebServlet("/DoRegister")
public class DoRegister extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DoRegister() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String msg = "";
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		String password2=request.getParameter("password2");
		if(password.equals(password2)){
			member pu=new member();
			pu.setName(username);
			pu.setPassword(password2);
			
			MenberUserService p=new MenberUserService();
		
			boolean bl = p.idExist(username);
			if(bl) {
				msg = "该用户已存在";
				request.setAttribute("msg", msg);
				request.getRequestDispatcher("ToRegister").forward(request, response);
			}else {
				int i = p.addAccount(pu);
				if(i == 1) {
					msg = "注册成功";
					request.setAttribute("msg", msg);
					request.getRequestDispatcher("/ToLogin").forward(request, response);
				}else {
					msg = "注册失败";
					request.setAttribute("msg", msg);
					request.getRequestDispatcher("/ToRegister").forward(request, response);
				}
			}
			
		}else{
			msg = "两次密码不一致";
			request.setAttribute("msg", msg);
			request.getRequestDispatcher("/ToLogin").forward(request, response);
			
		}
			
	}
		
		
	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
